package cn.itcast.dagger2demo;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private TextView mTextViewContent;

    @Inject
    DaggerPrecenter daggerPrecenter;
    @Inject
    OkHttpClient okHttpClient;
    @Inject
    Retrofit retrofit;
    private RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextViewContent = ((TextView) findViewById(R.id.textview_content));

        AppComponent appComponent = ((MyApplication) getApplication()).getAppComponent();

        DaggerActivityComponent.builder()
                .appComponent(appComponent)
                .activityModule(new ActivityModule(this))
                .build()
                .inject(this);

        requestQueue = Volley.newRequestQueue(this);




        Log.i("onCreate", "okHttpClient = [" + okHttpClient + "]");
        Log.i("onCreate", "retrofit = [" + retrofit + "]");

        daggerPrecenter.setUser();
    }

    public void showUserName(String name) {

        mTextViewContent.setText(name);
    }

    public void volleyRequest(View view) {

        Uri uri = Uri.parse("http://192.168.209.1:8080/market/favorites").buildUpon()
                .appendQueryParameter("pId", "1")
                .appendQueryParameter("page", "0")
                .appendQueryParameter("pageNum", "10")
                .build();

        StringRequest stringRequest = new StringRequest(
                Request.Method.GET,
                uri.toString()
                , new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                mTextViewContent.setText(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                int statusCode = error.networkResponse.statusCode;


                //Log.d("MainActivity", error.getMessage());

                Log.d(TAG, "onErrorResponse() statusCode："+statusCode);
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {

                HashMap<String, String> map = new HashMap<>();
                map.put("userid", "2042822");
                return map;
            }

            /*@Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return super.getParams();
            }*/
        };

        requestQueue.add(stringRequest);
    }
}
